<?php
namespace app\common\model;

use think\Model;

/**
 * 用户token模型
 * Class AdminUser
 * @package app\common\model
 */
class UserBehaviorLog extends Model
{

    /**
     * 根据用户id查询用户的token
     *
     * @author mayy
     * @date 2017-8-14
     * @param int $user_id
     * @return array
     */
    public function getUserBehavior($user_id)
    {
        $return = [];
        if ($user_id <= 0) {
            return $return;
        }

        $res = $this->where(['user_id' => $user_id])->select();
        if (!empty($res)) {
             $return = $res->toArray();
        }
        return $return;
    }


    /**
     * 插入一条日志
     *
     * @author mayy
     * @date 2017-8-14
     * @param array $req
     * @return bool | integer
     */
    public function recordBehaviorLog(array $req)
    {
        if (empty($req)) {
            return false;
        }

        $data = $req;
        $data['addtime'] = NOW_DATE;

        $res = $this->allowField(true)->data($data,true)->isUpdate(false)->save();
        $return = ($res && isset($this->id) && $this->id > 0) ? $this->id : false;
        return $return;
    }
}